package com.hsy.sunflowerbao.home.ui

import android.support.v7.widget.RecyclerView
import android.view.*
import com.flux.ui.adapter.FFragmentRecycleAdapter
import com.flux.utils.GlideUtil
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.ui.adapter.BaseHolder
import com.hsy.sunflowerbao.home.data.HomeData
import kotlinx.android.synthetic.main.item_home.view.*
import java.lang.ref.WeakReference

/**
 * 首页适配器
 * Created on 2018/3/26
 *
 * @author Yang
 */
class HomeAdapter(fragment: ListFragment, var items: List<HomeData>) : FFragmentRecycleAdapter(WeakReference(fragment)) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = BaseHolder(getItemView(R.layout.item_home, parent))

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        GlideUtil.loadCircle(items[position].user.avatar, holder.itemView.homeUserAvatar, R.drawable.default_avatar, fragment = mFragment.get())
        holder.itemView.homeUserName.text = if (items[position].user.nick_name.isEmpty()) "未设置昵称" else items[position].user.nick_name
        holder.itemView.homeUserPhone.text = items[position].user.user_name
        holder.itemView.homeName.text = items[position].name
        holder.itemView.homeNum.text = mFragment.get()?.getString(R.string.txt_home_num, items[position].num)
        if (items[position].type == "1") {
            holder.itemView.homeTypeCoupon.visibility = View.VISIBLE
            holder.itemView.homeTypeScore.visibility = View.GONE
            holder.itemView.homeNum.setTextColor(-0x86ce2)
        } else {
            holder.itemView.homeTypeCoupon.visibility = View.GONE
            holder.itemView.homeTypeScore.visibility = View.VISIBLE
            holder.itemView.homeNum.setTextColor(-0xff5094)
        }
    }

    override fun getItemCount() = items.size
}